package cn.com.tuns.assess.qsb.frame.widget;

import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import cn.com.tuns.assess.qsb.R;


public class CenterDialog {

	private Activity activity;

	private View main;

	private DialogUtil dialogUtil;

	private boolean cancelable = true;

	public CenterDialog(Activity activity) {
		this.activity = activity;
		cancelable = true;
	}

	public CenterDialog createDialog(String message,boolean showTitle, String[] text,
                                     final OnClickListener[] onClick) {
		View view = View.inflate(activity, R.layout.center_dialog_main, null);
		TextView textView = (TextView) view.findViewById(R.id.message);
		TextView title = (TextView) view.findViewById(R.id.title);
		if (message == null || message.length() == 0)
			textView.setVisibility(View.GONE);
		else
			textView.setText(message);

		title.setVisibility(showTitle?View.VISIBLE:View.GONE);

		View left = view.findViewById(R.id.left);
		View right = view.findViewById(R.id.right);
		if (text.length == 0) {
			left.setVisibility(View.GONE);
			right.setVisibility(View.GONE);
		} else if (text.length == 1) {
			left.setVisibility(View.GONE);
			textView = (TextView) right.findViewById(R.id.text_right);
			textView.setText(text[0]);
			right.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					dismiss();
					if (onClick[0] != null)
						onClick[0].onClick(v);
				}
			});
		} else {
			textView = (TextView) left.findViewById(R.id.text_left);
			textView.setText(text[0]);
			left.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					dismiss();
					if (onClick[0] != null)
						onClick[0].onClick(v);
				}
			});

			textView = (TextView) right.findViewById(R.id.text_right);
			textView.setText(text[1]);
			right.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					dismiss();
					if (onClick[1] != null)
						onClick[1].onClick(v);
				}
			});
		}

		main = view;
		return this;
	}

	public void show() {
		// View root = activity.findViewById(R.id.main);
		choose(main);
		// dialogUtil.show(root, Gravity.CENTER);
	}

	// public void show(View root) {
	// choose(main);
	// // dialogUtil.show(root, Gravity.CENTER);
	// }

	public void dismiss() {
		if (dialogUtil != null)
			dialogUtil.dismiss();
		dialogUtil = null;
	}

	public static interface OnItemClick {
		public void onItemClick(View view, int index);
	}

	public void setCancelable(boolean cancelable) {
		this.cancelable = cancelable;
	}

	public void choose(View show) {
		dialogUtil = new DialogUtil(activity, show, DialogUtil.Type.Center, cancelable);
		dialogUtil.show();

	}

	public static void showSimpleDialog(String str, Activity activity) {
		CenterDialog centerDialog = new CenterDialog(activity);
		centerDialog.createDialog(str,false,
				new String[] { activity.getString(R.string.qd) },
				new OnClickListener[] { null });
		centerDialog.show();
	}

}
